<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名">
            {{ dataForm.realName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门名称">
            {{ dataForm.deptName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="职别">
          {{ dataForm.level }}
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="入厂日期">
          {{ dataForm.companyTime }}
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="申请离职时间">
          {{ dataForm.takeTime }}
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="确定离职时间">
          {{ dataForm.leaveTime }}
        </el-form-item>
        </el-col>
        <el-col :span="12">

        <el-form-item label="离职原因">
          {{ dataForm.content }}
        </el-form-item>
</el-col>
</el-row>

        <el-form-item label="审核" prop="isPass">

          <el-radio-group v-model="dataForm.isPass">
            <el-radio label="1">待处理</el-radio>
            <el-radio label="2">通过</el-radio>
            <el-radio label="3">审核不通过</el-radio>
          </el-radio-group>

        </el-form-item>

        <el-form-item label="审核不通过内容" prop="passContent" v-if="dataForm.isPass == 3">
          <el-input rows="6" v-model="dataForm.passContent" type="textarea" placeholder="审核不通过内容"></el-input>
        </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '', userId: '', type: '', contentDicCode: '', contentType: '', content: '', imgId: '', imgUrl: '', sort: '', isPass: '', passContent: '', createTime: '', updateTime: '', companyId: '', companyName: '', realName: '', userPhone: ''
});

const rules = ref({
  userId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  contentDicCode: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  contentType: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  imgId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  imgUrl: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  sort: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  isPass: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  passContent: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  createTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  updateTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  companyId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  companyName: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  userPhone: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/userdata/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/api/userdata", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
